home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* multisample.c - shows how to use multisampling to antialias
- *
- * Escape key - exit the program
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mousebutton - change the twist angle based on
- * horizontal mouse movement
- * Right Mousebutton - zoom in and out based on vertical
- * mouse movement
- * m Key - toggle multisampling on / off
- * p Key - toggle polygon offset on / off
- * R key - reset view
- */
- #include <math.h>
- #include <stdio.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
- void printHelp( char * );
-
- void polygonOffset( GLvoid );
- void multisampling( GLvoid );
-
- void createTorusLists(void);
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLint action;
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
-
- static GLint xStart = 0, yStart = 0;
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- static GLfloat offsetscale = 0.5;
- static GLfloat offsetbias = 0.002;
-
- static GLuint surfaceList, gridList;
-
- static GLboolean multisample_support = GL_TRUE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet(GLUT_SCREEN_WIDTH);
- height = glutGet(GLUT_SCREEN_HEIGHT);
- glutInitWindowPosition( width / 4, height / 4);
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH|GLUT_MULTISAMPLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrate polygon offset "
- "extension\n\n"
- "Axes: X - red, Y - green, Z - blue\n\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "Escape Key - exit the program\n"
- "m Key - toggle multisampling on / off\n"
- "p Key - toggle polygon offset on / off\n"
- "R Key - reset view\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLdouble maxObjectSize;
- float lmodel_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
-
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
-
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
- glEnable( GL_DEPTH_TEST );
-
- if (!glutExtensionSupported("GL_SGIS_multisample")) {
- fprintf(stderr,
- "\nGL_SGIS_multisample not supported on this machine\n");
- multisample_support = GL_FALSE;
- }
- if (!glutExtensionSupported("GL_EXT_polygon_offset")) {
- fprintf( stderr,
- "GL_EXT_polygon_offset not supported on this machine\n");
- }
- #ifdef GL_EXT_polygon_offset
- glPolygonOffsetEXT( offsetscale, offsetbias );
- #endif
- createTorusLists();
-
- /* Set up near and far, and determine the viewing distance */
- maxObjectSize = 4;
- near = 5.0;
- far = near + 2*maxObjectSize;
- resetView();
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'm':
- multisampling();
- glutPostRedisplay();
- break;
- case 'p':
- polygonOffset();
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- aspect = (GLdouble) width / (GLdouble) height;
- gluPerspective( 45.0, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- polygonOffset( GLvoid )
- {
- static GLboolean polyOffsetFlag = GL_FALSE;
-
- #ifdef GL_EXT_polygon_offset
- polyOffsetFlag = !polyOffsetFlag;
-
- /* Toggle polygon offset */
- if ( polyOffsetFlag == GL_TRUE ) {
- glEnable(GL_POLYGON_OFFSET_EXT);
- } else {
- glDisable(GL_POLYGON_OFFSET_EXT);
- }
- printf("polygon offset %s\n",
- (polyOffsetFlag?"enabled":"disabled"));
- #else
- printf("GL_EXT_polygon_offset not supported\n");
- #endif
- }
-
- GLvoid
- multisampling( GLvoid )
- {
- static GLboolean multisampleFlag = GL_FALSE;
-
- #ifdef GL_SGIS_multisample
- if ( multisample_support ) {
- multisampleFlag = !multisampleFlag;
-
- /* Toggle multisampling */
- if ( multisampleFlag == GL_TRUE ) {
- glEnable( GL_MULTISAMPLE_SGIS );
- } else {
- glDisable( GL_MULTISAMPLE_SGIS );
- }
- printf("multisampling %s\n",
- (multisampleFlag ?"enabled":"disabled"));
- } else
- printf("GL_SGIS_multisample not supported\n");
- #else
- printf("GL_SGIS_multisample not supported\n");
- #endif
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- /* Clear the depth buffer in addition to the window */
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, twistAngle );
- XYZaxes();
-
- glCallList( surfaceList );
- glCallList( gridList );
- glPopMatrix();
- glutSwapBuffers();
-
- checkError("drawScene");
- }
-
- void gridmaterials(void)
- {
- static float front_mat_diffuse[] = { 1.0, 1.0, 0.4, 1.0 };
- static float front_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
- static float back_mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
- static float back_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
-
- glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
- glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
- glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
- glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
- }
-
- void surfacematerials(void)
- {
- static float front_mat_diffuse[] = { 0.2, 0.7, 0.7, 1.0 };
- static float front_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
- static float back_mat_diffuse[] = { 1.0, 1.0, 0.2, 1.0 };
- static float back_mat_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
-
- glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
- glMaterialfv(GL_FRONT, GL_AMBIENT, front_mat_ambient);
- glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
- glMaterialfv(GL_BACK, GL_AMBIENT, back_mat_ambient);
- }
-
- /*
- * Control points of a torus in NURBS form. Can be rendered using
- * the GLU NURBS routines.
- */
- static GLfloat torusnurbpts[] = {
- 4.0, 0.0, 0.0, 4.0, 2.0, 0.0, 1.0, 2.0, 4.0, 0.0, 1.0, 2.0,
- 8.0, 0.0, 0.0, 4.0, 4.0, 0.0,-1.0, 2.0, 2.0, 0.0,-1.0, 2.0,
- 4.0, 0.0, 0.0, 4.0, 2.0,-2.0, 0.0, 2.0, 1.0,-1.0, 0.5, 1.0,
- 2.0,-2.0, 0.5, 1.0, 4.0,-4.0, 0.0, 2.0, 2.0,-2.0,-0.5, 1.0,
- 1.0,-1.0,-0.5, 1.0, 2.0,-2.0, 0.0, 2.0,-2.0,-2.0, 0.0, 2.0,
- -1.0,-1.0, 0.5, 1.0,-2.0,-2.0, 0.5, 1.0,-4.0,-4.0, 0.0, 2.0,
- -2.0,-2.0,-0.5, 1.0,-1.0,-1.0,-0.5, 1.0,-2.0,-2.0, 0.0, 2.0,
- -4.0, 0.0, 0.0, 4.0,-2.0, 0.0, 1.0, 2.0,-4.0, 0.0, 1.0, 2.0,
- -8.0, 0.0, 0.0, 4.0,-4.0, 0.0,-1.0, 2.0,-2.0, 0.0,-1.0, 2.0,
- -4.0, 0.0, 0.0, 4.0,-2.0, 2.0, 0.0, 2.0,-1.0, 1.0, 0.5, 1.0,
- -2.0, 2.0, 0.5, 1.0,-4.0, 4.0, 0.0, 2.0,-2.0, 2.0,-0.5, 1.0,
- -1.0, 1.0,-0.5, 1.0,-2.0, 2.0, 0.0, 2.0, 2.0, 2.0, 0.0, 2.0,
- 1.0, 1.0, 0.5, 1.0, 2.0, 2.0, 0.5, 1.0, 4.0, 4.0, 0.0, 2.0,
- 2.0, 2.0,-0.5, 1.0, 1.0, 1.0,-0.5, 1.0, 2.0, 2.0, 0.0, 2.0,
- 4.0, 0.0, 0.0, 4.0, 2.0, 0.0, 1.0, 2.0, 4.0, 0.0, 1.0, 2.0,
- 8.0, 0.0, 0.0, 4.0, 4.0, 0.0,-1.0, 2.0, 2.0, 0.0,-1.0, 2.0,
- 4.0, 0.0, 0.0, 4.0,
- };
-
- float circleknots[] = { 0.0, 0.0, 0.0, 0.25, 0.50, 0.50, 0.75, 1.0, 1.0, 1.0 };
-
- void
- createTorusLists(void)
- {
- GLUnurbsObj *nobj;
- int usegments=4;
- int vsegments=4;
-
- GLfloat modelView[16], projection[16];
- GLint viewport[4];
-
- glEnable(GL_AUTO_NORMAL);
-
- surfaceList = glGenLists(1);
- gridList = glGenLists(1);
-
- nobj = gluNewNurbsRenderer();
-
- gluNurbsProperty(nobj, GLU_SAMPLING_METHOD, GLU_DOMAIN_DISTANCE );
- gluNurbsProperty(nobj, GLU_U_STEP, (usegments-1)*4 );
- gluNurbsProperty(nobj, GLU_V_STEP, (vsegments-1)*4 );
- gluNurbsProperty(nobj, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
-
- glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
- glGetFloatv(GL_PROJECTION_MATRIX, projection);
- glGetIntegerv(GL_VIEWPORT, viewport);
- gluLoadSamplingMatrices(nobj, modelView, projection, viewport);
-
- gluNurbsProperty(nobj, GLU_DISPLAY_MODE, GLU_FILL);
- glNewList(surfaceList, GL_COMPILE);
- surfacematerials();
- gluBeginSurface(nobj);
- gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
- 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
- gluEndSurface(nobj);
- glEndList();
-
- gluNurbsProperty(nobj, GLU_DISPLAY_MODE, GLU_OUTLINE_POLYGON);
- glNewList(gridList, GL_COMPILE);
- gridmaterials();
- gluBeginSurface(nobj);
- gluNurbsSurface(nobj, 10, circleknots, 10, circleknots,
- 4, 28, torusnurbpts, 3, 3, GL_MAP2_VERTEX_4);
- gluEndSurface(nobj);
- glEndList();
- }
-